// pages/collectPage/collectPage.js
var {getGoodsDetail,getHomeGoods}=require("../../api/Home");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        collectCommodity: [], //收藏列表的商品详细
        needList:[],//推荐商品
    },
    //获取收藏列表中的所有商品
    getCommodity() {
        let collectLists = wx.getStorageSync('collectLists');
        this.setData({
            collectCommodity:collectLists
        })
    },
   // 收藏商品添加到购物车
   addShopping(options) {
    let id = options.target.dataset.id;
    let index = this.data.collectCommodity.findIndex(item => item.id == id);
    // console.log(index);
    let cartLists = {
        id: this.data.collectCommodity[index].id,
        goodsImage: this.data.collectCommodity[index].goodsImage,
        goodsName: this.data.collectCommodity[index].goodsName,
        goodsPrice: this.data.collectCommodity[index].goodsPrice,
        num: 1,
    };
    let goodsCarList = wx.getStorageSync('goodsCarList');
    if (goodsCarList) {
        let i = goodsCarList.findIndex(item => item.id == id);
        if (i != -1) {
            goodsCarList[i].num += 1;
        } else {
            goodsCarList.push(cartLists);
        }
        wx.setStorageSync('goodsCarList', goodsCarList);
    } else {
        wx.setStorageSync('goodsCarList', [cartLists])
    }
    wx.showToast({
        title: "已添加到购物车",
        duration:2000
      })
},
// 推荐商品添加到购物车
addRecommend(options) {
    let id = options.target.dataset.id;
    let index = this.data.needList.findIndex(item => item.id == id);
    let cartLists = {
        id: this.data.needList[index].id,
        goodsImage: this.data.needList[index].goodsImage,
        goodsName: this.data.needList[index].goodsName,
        goodsPrice: this.data.needList[index].goodsPrice,
        num: 1,
    };
    let goodsCarList = wx.getStorageSync('goodsCarList');
    if (goodsCarList) {
        let i = goodsCarList.findIndex(item => item.id == id);
        if (i != -1) {
            goodsCarList[i].num += 1;
        } else {
            goodsCarList.push(cartLists);
        }
        wx.setStorageSync('goodsCarList', goodsCarList);
    } else {
        wx.setStorageSync('goodsCarList', [cartLists])
    };
    this.setData({
        goodsList:wx.getStorageSync('goodsCarList')
    })
    wx.showToast({
        title: "已添加到购物车",
        duration:2000
      })
},
// 推荐商品
getHomeGoodsFun(){
    getHomeGoods().then(data=>{
        let lists=[];
       let goodslists=data.message.goods;
       goodslists.forEach(item=>{
           let {goods_id,goods_small_logo,goods_name,goods_price} = item;
           let data={
            id:goods_id,
            goodsName:goods_name,
            goodsPrice:goods_price,
            goodsImage:goods_small_logo,
            originalPrice:goods_price+100,
           };
           if(goods_small_logo){
               lists.push(data);
           }
       }) 
       this.setData({
        needList:lists
       })
    })
},
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCommodity();
        this.getHomeGoodsFun()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
this.getCommodity()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})